/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.controller;

import com.xy.biz.fd.domain.bo.DocumentFavoriteBO;
import com.xy.biz.fd.domain.converter.DocumentFavoriteConverter;
import com.xy.biz.fd.domain.query.DocumentFavoriteQuery;
import com.xy.biz.fd.domain.req.documentFavorite.GetDocumentFavoriteRequest;
import com.xy.biz.fd.domain.req.documentFavorite.SaveDocumentFavoriteRequest;
import com.xy.biz.fd.domain.resp.GetDocumentFavoriteResponse;
import com.xy.biz.fd.service.DocumentFavoriteService;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.PageRequest;
import com.xy.lang.domain.Result;
import com.xy.lang.util.CalendarUtils;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.annotation.Validatable;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * @author wcs
 * @date 2021-2-2
 */
@Api(tags = "文档收藏服务")
@Slf4j
@RestController
@RequestMapping("/api/fd/document_favorite")
public class DocumentFavoriteController {

    @Autowired
    private DocumentFavoriteService documentFavoriteService;

    @ApiOperation(value = "收藏文档")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveDocumentFavorite(@RequestBody SaveDocumentFavoriteRequest request) {
        DocumentFavoriteBO documentFavoriteBO = DocumentFavoriteConverter.toBO(request);
        documentFavoriteBO.setCreateUserId(UserContextHolder.getUserId());
        return documentFavoriteService.saveDocumentFavorite(documentFavoriteBO);
    }

    @ApiOperation(value = "文档取消收藏")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteDocumentFavorite(@RequestBody GetDocumentFavoriteRequest request) {
        documentFavoriteService.deleteDocumentFavorite(request.getDocumentFavoriteId());
        return Result.success();
    }

    @ApiOperation(value = "文档批量取消收藏")
    @Validatable
    @PostMapping(value = "/delete_by_ids")
    public Result<?> deleteListDocumentFavorite(@RequestBody Collection<Long> documentFavoriteIds) {
        documentFavoriteService.deleteListDocumentFavorite(documentFavoriteIds);
        return Result.success();
    }

    @PostMapping(value = "/page")
    public Result<Page<GetDocumentFavoriteResponse>> pageDocumentFavorite(@RequestBody PageRequest request) {
        DocumentFavoriteQuery query = new DocumentFavoriteQuery(request.getPageIndex(), request.getPageSize(), UserContextHolder.getUserId());
        List<GetDocumentFavoriteResponse> list = DocumentFavoriteConverter.toResponse(documentFavoriteService.listDocumentFavorite(query));
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(e -> e.setCreateTimeString(CalendarUtils.getDeadlineString(e.getCreateTime(), LocalDateTime.now())));
        }
        int count = documentFavoriteService.countDocumentFavorite(query);
        return Result.success(new Page<>(list, count));
    }
}
